class Solution {
public:
    int minOperations(vector<int>& target, vector<int>& arr) {
        int n = target.size();
        int m = arr.size();
        unordered_map<int,int> mp;
        for(int i = 0;i < n;i++){
            mp[target[i]] = i; 
        }
        
        int Max = 0;
        vector<int> ans;
        for(int i = 0;i < m;i++){
            if(mp.count(arr[i])){
                int idx = mp[arr[i]];
                auto p = lower_bound(ans.begin(),ans.end(),idx);
                if(p == ans.end()){
                    ans.push_back(idx);
                }else{
                    *p = idx;
                }
            }
        }
        return n - ans.size();
    }
};
